<?php

class LocationController extends Zend_Controller_Action
{

    public function init()
    {
    	$this->view->headTitle("Location");
    }

    public function indexAction()
    {
        $locMapper=new Model_LocationMapper();
        $data=$locMapper->getAllLocations();
        $this->view->locations=$data;
    }

    public function detailAction(){
    	$id=$this->_getParam("id");
    	try {
    		$locMapper=new Model_LocationMapper();
    		$loc=$locMapper->getLocation($id);
    		$loc->setId($id);
    		$this->view->loc=$loc;
    		
    		$dish_locMapper=new Model_DishLocationMapper();
    		$dishes=$dish_locMapper->getAllDishOfLocation($id,$loc);
    		
    		$this->view->dishes=$dishes;
    		$loc->setCats($locMapper->getCategoriesOfLocation($loc));
    		
    		if ($loc->getDistrict()!=null)
    			$this->view->district=$loc->getDistrict()->getName();
    		$this->view->error=false;
    	}
    	catch(Exception $e) {
    		$this->view->error=true;
    	}
    }
    
    public function managerAction()
    {
    	if (Model_Role::IsDenied()){
    		$this->_redirect('/error/deny');
    	}
    
    	// action body
    	$locMapper=new Model_LocationMapper();
    	$data=$locMapper->getAllLocations();
    	$this->view->locations=$data;
    }
    
    public function createAction()
    {
    	$disM=new Model_DistrictMapper();
    	$dis=$disM->getAllDistricts();
    	$this->view->districts=$dis;
    	 
    	 
    }
    
    public function createprocessAction()
    {
    	if ($this->_request->isPost())
    	{
    		$name=$this->_getParam("location-name");
    		$disid=$this->_getParam("location-district");
    		$add=$this->getParam("location-address");
    		$phone=$this->getParam("location-phone");
    		$image=$this->_getParam("location-image");
    		$des=$this->_getParam("location-description");
    		$star=$this->getParam("location-rating");
    		 
    		$error="";
    		 
    		$loc=new Model_Location();
    		$dis=new Model_District();
    		$dis->setId($disid);
    		$loc->setName($name)->setDistrict($dis)->setDescription($des)->setImage($image)->setAddress($add)->setPhone($phone)->setStar($star);
    		$locM=new Model_LocationMapper();
    		$result=$locM->createLocation($loc);
    		 
    		if ($result=="error")
    		{
    			$this->_redirect('/location/create');
    		}
    		else
    		{
    			$this->_redirect('/location/index');
    		}
    		 
    		 
    	}
    }
    
    public function editAction()
    {
    	$id=$this->_getParam("id");
    	try {
    		$locMapper=new Model_LocationMapper();
    		$loc=$locMapper->getLocation($id);
    		$this->view->location=$loc;
    		$this->view->id=$id;
    		$this->view->name=$loc->getName();
    		$this->view->description=$loc->getDescription();
    		$this->view->image=$loc->getImage();
    		$this->view->address=$loc->getAddress();
    		$this->view->phone=$loc->getPhone();
    		$this->view->star=$loc->getStar();
    
    		$disM=new Model_DistrictMapper();
    		$districts=$disM->getAllDistricts();
    		$this->view->districts=$districts;
    
    		if ($loc->getDistrict()!=null)
    		{
    			$this->view->district=$loc->getDistrict()->getName();
    			$this->view->districtId=$loc->getDistrict()->getId();
    		}
    		 
    		$this->view->error=false;
    	}
    	catch(Exception $e) {
    		$this->view->error=true;
    	}
    
    
    }
    
    public function editprocessAction(){
    	$id=$this->_getParam("id");
    	$name=$this->_getParam("location-name");
    		$disid=$this->_getParam("location-district");
    		$add=$this->getParam("location-address");
    		$phone=$this->getParam("location-phone");
    		$image=$this->_getParam("location-image");
    		$des=$this->_getParam("location-description");
    		$star=$this->getParam("location-rating");
    	 
    	$error="";
    	 
    	$loc=new Model_Location();
    		$dis=new Model_District();
    		$dis->setId($disid);
    		$loc->setName($name)->setDistrict($dis)->setDescription($des)->setImage($image)->setAddress($add)->setPhone($phone)->setStar($star)->setId($id);
    		$locM=new Model_LocationMapper();
    		$result=$locM->updateLocation($loc);
    		$this->_redirect('/location/manager');
    	
    }
    public function removeAction()
    {
    	$id=$this->_getParam("id");
    	$locM=new Model_LocationMapper();
    	$result=$locM->removeLocation($id);
    	 
    	$this->_redirect('/location/index');
    	 
    }
    
    

}

